#import "LightBarIconButton.h"

@implementation LightBarIconButton

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.highlightColor = [UIColor colorWithRed:1 green:0.85 blue:0.2 alpha:1]; // 明黄色
        self.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightSemibold];
        self.tintColor = [UIColor whiteColor];
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self setTitleColor:self.highlightColor forState:UIControlStateSelected];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return self;
}

- (void)setIcon:(UIImage *)icon title:(NSString *)title {
    [self setImage:icon forState:UIControlStateNormal];
    [self setTitle:title forState:UIControlStateNormal];
    [self layoutIfNeeded];
}

// 让图片和文字垂直居中
- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat spacing = 4; // 图标和文字间距
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.intrinsicContentSize;
    CGFloat totalHeight = imageSize.height + spacing + titleSize.height;

    self.imageEdgeInsets = UIEdgeInsetsMake(-(totalHeight - imageSize.height), 0, 0, -titleSize.width);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, -(totalHeight - titleSize.height), 0);
}

// 选中时icon高亮
- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    if (selected) {
        self.tintColor = [UIColor colorWithRed:1 green:0.85 blue:0.2 alpha:1];
        [self setTitleColor:[UIColor colorWithRed:1 green:0.85 blue:0.2 alpha:1] forState:UIControlStateNormal];
    } else {
        self.tintColor = [UIColor whiteColor];
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}

@end 